package rush.edu.wechat.web.utils;

import org.springframework.util.StringUtils;
import rush.edu.wechat.common.config.PublicConfig;

import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * DESC: 路由权限工具类
 * USER:hmily
 * DATE:2018/2/1
 * TIME:10:11
 */
public class UriAuthUtils {

    public static boolean isFree(String uri){
        Map<String, String> annonUriMap = PublicConfig.annonUri;
        Set<String> set = annonUriMap.keySet();
        String[] splitUri = splitUri(uri);
        if (null != splitUri){
            Stream<String> filterUri = set.stream().filter(s -> s.startsWith("uri"));
            if (filterUri.count() > 0)
                return true;
            Stream<String> filterFatherUri = set.stream().filter(s -> s.startsWith("/"+splitUri[0]));
            String[] objects = (String[])filterUri.toArray();

            for (String s : objects){
                String[] splitUri1 = splitUri(s);
                //Arrays.
                if (splitUri1.length <=1)
                    continue;
                if (splitUri1[1].equals("**"))
                    return true;
            }

        }
        return false;
    }


    public static String[] splitUri(String uri){
        if (!StringUtils.isEmpty(uri)){
           return uri.split("/");
        }else
            return null;
    }
}
